package com.base.myproject.client.busi.b2c;

import com.base.myproject.client.busi.b2c.shopping.ShoppingCarPanel;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.Register;
import com.base.myproject.client.tools.SendServlet;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;


public class B2cLogin extends com.base.myproject.client.system.Login{
	Dialog logindialog = new Dialog();
	public B2cLogin()
	{
		super();
	}
	public Dialog CreateLoginDialog()
	{
		logindialog.setModal(true);
		logindialog.setHeading("登录");
		logindialog.setButtons(Dialog.OK);
		logindialog.setSize(400, 150);
		layoutcontainer.setMonitorWindowResize(true);
		
		layoutcontainer.setStyleAttribute("background-color", "#ccd9e8");
		
		
		layoutcontainer.setLayout(new CenterLayout());  
		
		loginpanel.setHeading("登录");
		loginpanel.setFrame(false);
		loginpanel.setHeaderVisible(false);
		loginpanel.setAutoWidth(true);
		loginpanel.setAutoHeight(true);
		firstName.setFieldLabel("名字");
		firstName.setName("name");
		firstName.setAllowBlank(false);
		loginpanel.add(firstName, formData);

	
		pass.setFieldLabel("密码");
		pass.setName("pass");
		pass.setPassword(true);
		pass.setAllowBlank(false);
		loginpanel.add(pass, formData);

		Button b = new Button("确认");
		//loginpanel.addButton(b);
		//loginpanel.addButton(new Button("取消"));
		Button newuser = new Button("申请新用户");
		//loginpanel.addButton(newuser);
		
		loginpanel.setButtonAlign(HorizontalAlignment.CENTER);
		logindialog.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				login();
				
			}
			
		});
		newuser.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				Register r = new Register();
				r.getRegDialog().show();
			}
		});
		  KeyNav kn = new KeyNav(logindialog)
		    {
		    	public void onKeyPress(ComponentEvent ce) {  
		    		if(ce.getKeyCode()==13)
		    		{
		    			login();
		    		}
		    	}
		    };
		    layoutcontainer.setAutoHeight(true);
		    layoutcontainer.setAutoWidth(true);
		layoutcontainer.add(loginpanel);
		logindialog.add(layoutcontainer);
		//logindialog.addButton(b);
		logindialog.addButton(newuser);
		return logindialog;
	}
public LayoutContainer createForm() {
		
		layoutcontainer.setMonitorWindowResize(true);
		
		layoutcontainer.setStyleAttribute("background-color", "#ccd9e8");
		
		
//		layoutcontainer.setHeight(Window.getClientHeight());
//		layoutcontainer.setWidth(Window.getClientWidth());
		
		layoutcontainer.setLayout(new CenterLayout());  
	
		loginpanel.setHeading("登录");
		loginpanel.setFrame(true);
		loginpanel.setWidth(350);

		
		firstName.setFieldLabel("名字");
		firstName.setName("name");
		firstName.setAllowBlank(false);
		loginpanel.add(firstName, formData);

	
		pass.setFieldLabel("密码");
		pass.setName("pass");
		pass.setPassword(true);
		pass.setAllowBlank(false);
		loginpanel.add(pass, formData);

		Button b = new Button("确认");
		loginpanel.addButton(b);
		loginpanel.addButton(new Button("取消"));
		Button newuser = new Button("申请新用户");
		loginpanel.addButton(newuser);
		
		loginpanel.setButtonAlign(HorizontalAlignment.CENTER);
		b.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				login();
				
			}
			
		});
		newuser.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				Register r = new Register();
				r.getRegDialog().show();
			}
		});
		  KeyNav kn = new KeyNav(loginpanel)
		    {
		    	public void onKeyPress(ComponentEvent ce) {  
		    		if(ce.getKeyCode()==13)
		    		{
		    			login();
		    		}
		    	}
		    };
		layoutcontainer.add(loginpanel);
		return layoutcontainer;

	}
static String cname;
protected	boolean login()
	{

		
		if(!loginpanel.isValid())
		{
			return false;
		}
		SendServlet send = new SendServlet();
		
		String str = send.getStringx("/"+GWT.getModuleName()+"/BaseServlet", "Action=ccodelogin&"+firstName.getName()+"="+firstName.getValue()+"&"+pass.getName()+"="+pass.getValue());
//		System.out.println("-"+str+"-"+Env.LOGINSUCCESS);
//		System.out.println(str==Env.LOGINSUCCESS);
//		System.out.println(str.equals(Env.LOGINSUCCESS));
		Document doc = XMLParser.parse((String) str);
		  NodeList list = doc.getElementsByTagName("root");
		  Node node = list.item(0);
		  Element elem = (Element) node;
		//参见服务器的格式
		  //服务器返回一个不重复的uid,客户端每次发送数据的时候到要同时发送uid
		if( elem.getAttribute("status").equals(Env.LOGINSUCCESS))
		{
			
			String id = elem.getAttribute("id");
			cname = elem.getAttribute("cname");
			String ccode = elem.getAttribute("ccode");
	
			if(id!=null)
			{
			Env.setUid(id);
			//Env.setEnvname(cname);
			
			Env.setCcode(ccode);
			Env.setStatus(Env.CCODELOGIN);
			
			logindialog.setVisible(false);
			logindialog.hide();
			//MessageBox.alert("提示", "成功登录！", null);
			Loginsuccess();
			return true;
			}
			else
			{
				MessageBox.alert("提示", "登录出错！", null);
			}
		}
		else{
			MessageBox.alert("提示", "登录名或者密码录入不正确！", null);
		}
	return false;
	}
private static void LoginSuccess_showUserInfo()
{
	UserInfoPanel uip = new UserInfoPanel(Env.getCcode());
	MainPage.getLeftpanel2().removeAll();
	MainPage.getLeftpanel2().add(uip);
	MainPage.getLeftpanel2().layout();
	
}
private static void Loginsuccess()
{
	LoginSuccess_showUsershopcar();
	LoginSuccess_showUserInfo();
}
	private static void LoginSuccess_showUsershopcar()
	{
		MainPage.getToppanelright().removeAll();
		HorizontalPanel hp = new HorizontalPanel();
		
		Button shoppingcar = MainPage.getShoppingcar();
		
		shoppingcar.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				ShoppingCarPanel scp = new ShoppingCarPanel(Env.getCcode());
				MainPage.getCenterpanel().removeAll();
				MainPage.getCenterpanel().add(scp);
				MainPage.getCenterpanel().layout();
			}
			
		});
		Html html =new Html();
		html.setHtml("用户名："+cname);
		Button logout = new Button("退出");
		logout.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				B2cLogout logout = new B2cLogout();
				if(logout.logout())
				{
					MainPage.CreateTopPanel();
				}
			}
			
		});
		hp.add(shoppingcar);
		hp.add(html);
		hp.add(logout);
		MainPage.getToppanelright().add(hp);
		MainPage.getToppanelright().layout();
	}

}
